$(function () {
    $(".ordinaryFast").Validform({
        btnSubmit: "#next_btn",
        showAllError: true,
        tiptype: 3
    });
    $(".secondStep").Validform({
        btnSubmit: "#register_btn",
        showAllError: true,
        tiptype: 3
    });
    $('.btn').click(function () { //明码密码切换
        var _that = $(this);
        var password_type = $(this).siblings('input').attr('type');
        if (password_type == 'password') {
            _that.siblings('input').attr('type', 'text');
            _that.children('img').attr('src', '/cn/images/icon-passWordSee.png');
        } else if (password_type == 'text') {
            _that.siblings('input').attr('type', 'password');
            _that.children('img').attr('src', '/cn/images/icon-passWordNoSee.png');
        }
    })
});

function nextStep() { //注册下一步
    var userName = $('.userName').val();
    var verificationCode = $('.loginCode').val();
    if (verificationCode == "") {
        return false;
    }
    if (userName == "") {
        return false;
    }
    $('.ordinaryFast').hide();
    $('.secondStep').slideDown(400);

    _czc.push(['_trackEvent','注册','下一步','',''])
};
//注册
function register(obj) {
    // var nickName = $('.nickName').val();
    var userName = $('.userName').val();
    var url = $('.url').val();
    var verificationCode = $('.loginCode').val();
    var password = $('#password').val().replace(/\s+/g, "");
    var confirmPassword = $('#confirmPassword').val().replace(/\s+/g, "");
    var type = $("#register_btn").attr("data-type");
    // if (nickName == "") {
    //     return false;
    // }
    if(!$('#agreement').is(':checked')){
        alert('请勾选用户协议');
        return false;
    }
    if (password == "") {
        return false;
    }
    if (confirmPassword == "") {
        return false;
    }
    $.post('/cn/new-api/register', {
        type: type,
        registerStr: userName,
        code: verificationCode,
        pass: password,
        url: url
        // nickname: nickName
    }, function (re) {
        if (re.code == 1) {
            _czc.push(['_trackEvent','注册','注册成功','','']);
            localStorage.setItem('prop',1)
            alert('恭喜您注册成功');
            window.location.href = '/cn/index/index';

        } else {
            alert(re.message);
        }
    }, 'json')
};
//enter注册
function enterRegister() {
    if (event.keyCode == 13) {
        register();
    }
}


//监听
function watch() {
    if ($('.userName').val().length == 11) {
        $('.phoneCode').removeAttr('disabled');
        $('.phoneCode').addClass('phoneCodeVi');
    } else {
        $('.phoneCode').attr('disabled', true);
        $('.phoneCode').removeClass('phoneCodeVi');
    }
}
function watchEmail(){
    if ($('.userName').val().length != '') {
        $('.phoneCode').removeAttr('disabled');
        $('.phoneCode').addClass('phoneCodeVi');
    } else {
        $('.phoneCode').attr('disabled', true);
        $('.phoneCode').removeClass('phoneCodeVi');
    }
}
//发送验证码
function sendCode(obj) {
    var timeNum = 60;
    var phone = $('.userName').val();

    //type  1--注册  9--绑定  2--找回
    $.post('/cn/new-api/phone-code', {
        phoneNum: phone,
        type: 1
    }, function (re) {
        alert(re.message);
        if (re.code == 1) {
            $('.userName').attr('readonly', true);
            $(obj).attr('disabled', true);
            $(obj).removeClass('phoneCodeVi');
            $(obj).addClass('phoneCodeSi');
            var timer = setInterval(function () {
                $(obj).html("重新获取(" + (timeNum + 'S)'));
                timeNum--;
                if (timeNum < 0) {
                    clearInterval(timer);
                    $('.userName').removeAttr('readonly');
                    $(obj).removeAttr('disabled');
                    $(obj).addClass('phoneCodeVi');
                    $(obj).removeClass('phoneCodeSi');
                    $(obj).html("获取短信验证码");
                }
            }, 1000);
        }

    }, 'json');

    _czc.push(['_trackEvent','注册','获取验证码','',''])
}
function sendEmailCode(obj){
    var timeNum = 60;
    var email = $('.userName').val();
    //type  1--注册  9--绑定  2--找回
    $.post('/cn/new-api/send-mail', {
        email: email,
        type: 1
    }, function (re) {
        alert(re.message);
        if (re.code == 1) {
            $('.userName').attr('readonly', true);
            $(obj).attr('disabled', true);
            $(obj).removeClass('phoneCodeVi');
            $(obj).addClass('phoneCodeSi');
            var timer = setInterval(function () {
                $(obj).html("重新获取(" + (timeNum + 'S)'));
                timeNum--;
                if (timeNum < 0) {
                    clearInterval(timer);
                    $('.userName').removeAttr('readonly');
                    $(obj).removeAttr('disabled');
                    $(obj).addClass('phoneCodeVi');
                    $(obj).removeClass('phoneCodeSi');
                    $(obj).html("获取邮箱验证码");
                }
            }, 1000);
        }

    }, 'json');
}